package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.modul.user.activity.ResetPwdActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.RxTimerUtil;
import com.jilian.mydemo.utils.SPUtil;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.utils.Utils;
import com.jilian.mydemo.views.ClearEditText;

public class UpdatePhoneActivity extends BaseActivity {
    private ClearEditText etPhone;
    private ClearEditText etCode;
    private RelativeLayout rlGetCode;
    private TextView tvGetCode;
    private TextView tvOk;



    @Override
    protected void createViewModel() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_updatephone;
    }

    @Override
    public void initView() {
        setNormalTitle("修改手机号码", v -> finish());
        etPhone = (ClearEditText) findViewById(R.id.et_phone);
        etCode = (ClearEditText) findViewById(R.id.et_code);
        rlGetCode = (RelativeLayout) findViewById(R.id.rl_get_code);
        tvGetCode = (TextView) findViewById(R.id.tv_get_code);
        tvOk = (TextView) findViewById(R.id.tv_ok);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        tvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utils.checkPhoneNo(etPhone.getText().toString())) {
                    ToastUitl.showImageToastTips(getResources().getString(R.string.error_phone));
                    return;
                }
                getMsgCode();

            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(etPhone.getText().toString())){
                    tvGetCode.setEnabled(false);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                }
                else{
                    tvGetCode.setEnabled(true);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title));
                }
                updateEnableStatus();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                updateEnableStatus();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Utils.checkPhoneNo(etPhone.getText().toString())) {
                    ToastUitl.showImageToastTips(getResources().getString(R.string.error_phone));
                    return;
                }

                resetPhone();
            }
        });
    }

    private void resetPhone() {
        showLoadingDialog();
        Factoty.getViewModel(UserViewModel.class,UpdatePhoneActivity.this).updateUserPhone(etPhone.getText().toString(),etCode.getText().toString());
        Factoty.getViewModel(UserViewModel.class,UpdatePhoneActivity.this).getUpdatePhoneliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if(dto.isSuccess()){
                    finish();
                    ToastUitl.showImageToastSuccess("修改成功");
                    SPUtil.clearData(Constant.SP_VALUE.SP);
                    MyApplication.getInstance().clearAllActivitys();
                    Intent intent = new Intent(UpdatePhoneActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }
    private int time = 60;
    /**
     * 获取验证码
     */
    private void getMsgCode() {
        getLoadingDialog().showDialog();
        Factoty.getViewModel(UserViewModel.class,UpdatePhoneActivity.this).sendSms(etPhone.getText().toString(),2);
        Factoty.getViewModel(UserViewModel.class,UpdatePhoneActivity.this).getSmsliveData().observe(UpdatePhoneActivity.this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                getLoadingDialog().dismiss();
                if (dto.getCode() == Constant.Server.SUCCESS_CODE) {
                    ToastUitl.showImageToastSuccess(dto.getMsg());
                    tvGetCode.setText("验证码("+String.valueOf(time + "s")+")");
                    tvGetCode.setEnabled(false);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                    RxTimerUtil.interval(1000, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext() {
                            if (time == 1) {
                                tvGetCode.setEnabled(true);
                                tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title));
                                tvGetCode.setText("获取验证码");
                                RxTimerUtil.cancel();
                                time = 60;
                                //设置手机号可编辑
                                etPhone.setEnabled(true);
                                //恢复图标
                                etPhone.setClearIconVisible(true);
                            } else {
                                time--;
                                tvGetCode.setText("验证码("+String.valueOf(time + "s")+")");
                                tvGetCode.setEnabled(false);
                                tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                                //设置手机号不可编辑
                                etPhone.setEnabled(false);
                                //清除图标
                                etPhone.setClearIconVisible(false);
                            }

                        }
                    });
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }
    /**
     * 更新按钮的可用状态
     */
    private void updateEnableStatus() {
        if (!TextUtils.isEmpty(etPhone.getText().toString())
                && !TextUtils.isEmpty(etCode.getText().toString())
                ) {
            tvOk.setEnabled(true);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_normal);
        } else {
            tvOk.setEnabled(false);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_dark);
        }
    }
}
